package com.android.love.ui;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.android.love.R;
import com.android.love.adapter.OrderDetailAdapter;
import com.android.love.net.UriHelper;
import com.android.love.util.ActivityUtil;
import com.android.love.vo.Address;
import com.android.love.vo.Goods;
import com.android.love.vo.MyRequest;
import com.android.love.vo.Order;
import com.github.kevinsawicki.wishlist.ViewFinder;

public class MyOrderDetail_2 extends BasePageActivity {
	private ViewFinder finder;
	// 订单状态
	private TextView tv_order_status;
	private TextView tv_order_id;
	private TextView tv_order_price;
	private TextView tv_order_kuaidi_id;
	private TextView tv_order_kuaidi_compare;
	// private ImageView iv_zaixianzhifu;
	// private LinearLayout ll_cart_img;
	// private TextView tv_cart;
	// private Tex./tView tv_format;
	// private TextView tv_address_text;
	// private TextView tv_zhifu;
	// private RelativeLayout rl_zhifu;
	// private RelativeLayout rl_kuaidi;
	// private TextView tv_kuaidi;
	// private EditText et_liuyan;
	//
	// private DisplayImageOptions options;
	// private int showNum;
	//
	// private String[] pay_type = new String[] { "支付宝（推荐）", "银联支付" };
	// private String[] express_type = new String[] { "普通快递（免运费）", "其他快递方式" };
	// private List<Address> address_list = Collections.emptyList();
	// private AlertDialog singleDialog;
	private Order order;
	private ListView lv_order;
	private Goods goods;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View view = inflater.inflate(R.layout.activity_order_detail_2, null);
		finder = new ViewFinder(view);
		setContentView(view);
		process();
		setListener();
	}

	@Override
	protected Boolean loadBottomTab() {
		return false;
	}

	@Override
	protected void setListener() {
		super.setListener();
		finder.find(R.id.iv_top_meinv).setOnClickListener(this);
		lv_order.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 单个商品详情
				MyRequest req = new MyRequest();
				req.authority = UriHelper.REALM_NAME;
				req.path = UriHelper.URL_GOODS;
				req.paramers = generateParams(order.getGoods().get(position)
						.getGoods_id());
				getDataFromTask(context, GOODS, req, goods, true, false);

			}
		});
	}

	@Override
	protected void process() {
		super.process();
		Intent intent = getCurrentIntent();
		if (intent == null)
			finish(true);
		order = (Order) intent.getSerializableExtra("order_detail");
		hide(finder.imageView(R.id.iv_shuaxin));
		finder.setText(R.id.top_title, "订单详情");
		tv_order_status = finder.find(R.id.tv_order_status);
		tv_order_id = finder.find(R.id.tv_order_id);
		tv_order_price = finder.find(R.id.tv_order_price);
		tv_order_kuaidi_id = finder.find(R.id.tv_order_kuaidi_id);
		tv_order_kuaidi_compare = finder.find(R.id.tv_order_kuaidi_compare);
		changeStyle(order);
		lv_order = finder.find(R.id.lv_order);
		lv_order.setAdapter(new OrderDetailAdapter(context, loader, order
				.getGoods()));
	}

	/**
	 * 得到传递的参数
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public Map<String, String> generateParams(long goods_id) {
		JSONObject json = new JSONObject();
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("app_key", "android");
		params.put("v", "1.0");
		params.put("goods_id", goods_id + "");
		return params;
	}

	/**
	 * 改变样式
	 * 
	 * @param order
	 */
	private void changeStyle(Order order) {
		String html_status = null;
		switch (order.getStatus()) {
		case 1:
			// 订单未未支付
			hide(tv_order_kuaidi_id).hide(tv_order_kuaidi_compare);
			html_status = "<font color=\"#636363\">" + "订单状态： " + "</font>"
					+ "<font color=\"#ffcd00\">" + "等待付款" + "</font>";

			break;
		case 2:
			// 订单支付待发货
			html_status = "<font color=\"#636363\">" + "订单状态： " + "</font>"
					+ "<font color=\"#ffcd00\">" + "等待发货" + "</font>";
			break;
		case 3:
			// 订单已发货
			html_status = "<font color=\"#636363\">" + "订单状态： " + "</font>"
					+ "<font color=\"#ffcd00\">" + "已发货" + "</font>";
			break;
		case 4:
			// 订单已完成
			html_status = "<font color=\"#636363\">" + "订单状态： " + "</font>"
					+ "<font color=\"#ffcd00\">" + "已完成" + "</font>";
			break;
		case 5:
			// 订单已取消
			hide(tv_order_kuaidi_id).hide(tv_order_kuaidi_compare);
			html_status = "<font color=\"#636363\">" + "订单状态： " + "</font>"
					+ "<font color=\"#ffcd00\">" + "订单已取消" + "</font>";
			break;

		default:
			break;
		}
		tv_order_status.setText(Html.fromHtml(html_status));
		tv_order_id.setText("订单号: " + order.getOrder_id());
		String html_price = "<font color=\"#636363\">" + "订单金额：" + "</font>"
				+ "<font color=\"#e2017b\">" + "￥ " + order.getSum()
				+ "</font>";
		tv_order_price.setText(Html.fromHtml(html_price));
		tv_order_kuaidi_id.setText("快递单号：" + order.getExpress_id());
		tv_order_kuaidi_compare.setText("快递公司：" + order.getExpress_agency());
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_top_meinv:
			finish(true);
			break;

		default:
			break;
		}
	}

	@Override
	public void handle(int tag, Object object) {
		super.handle(tag, object);
		switch (tag) {
		case GOODS:
			if (object != null) {
				goods = (Goods) object;
				Intent intent = new Intent(context, DetailActivity.class);
				intent.putExtra("type", 1);
				intent.putExtra("singleGoods", goods);
				startActivity(intent);
				ActivityUtil.runActivityAnim(MyOrderDetail_2.this, false);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& keyCode == KeyEvent.KEYCODE_BACK) {
			finish(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
